import { createSlice, createAsyncThunk } from '@reduxjs/toolkit';

import { fetchNewsList } from '../request';
// 创建异步actionCreator
const getNewsList = createAsyncThunk(
    'getNewsList',      // actionType的前缀, 必须提供
    async () => {
        // 调用请求数据的方法
        const res = await fetchNewsList();
        return res.data;
    }
)

const newsSlice = createSlice({
    name: "news",
    initialState: {
        // 新闻列表
        newsList: []
    },
    // 同步actionCreator
    reducers: {

    },
    // 注册异步actionCreator
    extraReducers: (builder) => {
        builder.addCase(getNewsList.fulfilled, (state, action) => {
            console.log(action,'dynamic case');
            state.newsList = action.payload;
        });
    }
});
export const { getDataList } = newsSlice.actions;
export { getNewsList };
export default newsSlice.reducer;